/* client.cc:  */

#include "connection.h"
#include "connectionclosedexception.h"
#include "protocol.h"

#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>    /* for exit() and atoi() */

using namespace std;
using client_server::Connection;
using client_server::ConnectionClosedException;
/*
 *  */
void writeToServer(char value, Connection& conn) {
    conn.write(value);
}

/*
 * Read a string from the server.
 */
string readString(Connection& conn) {
    string s;
    char ch;
    while ((ch = conn.read()) != protocol::Protocol::ANS_END)  // is end of string
        s =+ ch;
    return s;
}
int toInt(const string &s){
		istringstream iss(s, stringstream::in);
		int i;
		iss >> i;
		return i;
	}

int main(int argc, char* argv[]) {
    if (argc != 3) {
        cerr << "Usage: myclient host-name port-number" << endl;
        exit(1);
    }
    
    Connection conn(argv[1], atoi(argv[2]));
    if (! conn.isConnected()) {
        cerr << "Connection attempt failed" << endl;
        exit(1);
    }
    
    cout << "Type your command: ";
    string command;
    while (cin >> command) {
	if(command.compare("L_NG") == 0){ //LIST_NEWSGROUPS:
		writeToServer(protocol::Protocol::COM_LIST_NG, conn);
		writeToServer(protocol::Protocol::COM_END,conn);
		cout << "Lista alla newsgroups" << endl;
	}else if(command.compare("C_NG") == 0){ //CREATE_NEWSGROUP: parameter name
		writeToServer(protocol::Protocol::COM_LIST_NG, conn);
		string groupName;
		cin >> groupName;
		cout << "Skapar en ny newsgroup med namnet "<< groupName << endl;
		const char *gn;
		gn = groupName.c_str();
			
		while( *gn != '\0'){
			writeToServer(*gn, conn);
			gn++;
		}
		writeToServer(protocol::Protocol::COM_END,conn);
	}else if(command.compare("D_NG") == 0){ //DELETE_NEWSGROUP: parameter newsgroupId
		writeToServer(protocol::Protocol::COM_DELETE_NG, conn);
		string groupId;
		cin >> groupId;
		cout << "Tabort newsgroupen med id:et " << groupId << endl;
		const char *gId;
		gId = groupId.c_str();
		while( *gId != '\0'){
			writeToServer(*gId, conn);
			gId++;
		}
		writeToServer(protocol::Protocol::COM_END,conn);
	}else if(command.compare("L_A") == 0){ //LIST_ARTICLES: parameter newsgroupId
		writeToServer(protocol::Protocol::COM_LIST_ART, conn);
		string groupId;
		cin >> groupId;
		const char *gId;
		gId = groupId.c_str();
		while( *gId != '\0'){
			writeToServer(*gId, conn);
			gId++;
		}
		writeToServer(protocol::Protocol::COM_END,conn);
		cout << "Listar artiklar i newsgroup:en "<< groupId << endl;
	}else if(command.compare("C_A") == 0){ //CREATE_ARTICLE: parameter newsgroupID, articleName, authorName, text
		string groupId;
		cin >> groupId;
		const char *gId;
		gId = groupId.c_str();
		while( *gId != '\0'){
			writeToServer(*gId, conn);
			gId++;
		}
		string artName;
		cin >> artName;
		const char *an;
		an = groupId.c_str();
		while( *an != '\0'){
			writeToServer(*an, conn);
			an++;
		}
		string authorName;
		cin >> authorName;
		const char *autn;
		autn = groupId.c_str();
		while( *autn != '\0'){
			writeToServer(*autn, conn);
			autn++;
		}
	//	const char *txt;
	//	while( txt != NULL){
	//		writeToServer(*txt, conn);
	//		txt++;
	//	}
		writeToServer(protocol::Protocol::COM_END, conn);
		cout << "Skapar en artikel med följande info " << groupId << " "<< artName << " " << authorName << " "<< endl;
	}else if(command.compare("G_A") == 0){//GET_ARTICLE: parameter newsgroupID, articleName
		writeToServer(protocol::Protocol::COM_GET_ART, conn);
		string groupId;
		cin >> groupId;
		const char *gId;
		gId = groupId.c_str();
		while( *gId != '\0'){
			writeToServer(*gId, conn);
			gId++;
		}
		string artName;
		cin >> artName;
		const char *artN;
		artN = groupId.c_str();
		while( *artN != '\0'){
			writeToServer(*artN, conn);
			artN++;
		}
		writeToServer(protocol::Protocol::COM_END,conn);
		cout << "Hämtar artikel: " << artName << " från newsgroup: "<< groupId << endl;
	}else{
		cout << "Wrong input stupad, plz try again ;-)" << endl;
}

	try {	//Läser från servern
		string answer = readString(conn);
		stringstream ss(answer);
		string command;
		ss >> command;
		if (toInt(command) == protocol::Protocol::ANS_LIST_NG){
           		string nbrOfGroups;
          	 	ss >> nbrOfGroups;
           		cout <<"The number of newsgroups are: " << nbrOfGroups << endl;
          	 	string name;
           		string groupId;
           		cout << "Newsgroups: " << "NewsgroupId: " << endl;
                    	while( ss >> name){
           			ss >> groupId;
           			cout << name << " " << groupId << endl;
           		}
		}else if(toInt(command) == protocol::Protocol::ANS_CREATE_NG){
			string response;
			ss >> response;
			if(toInt(response) == protocol::Protocol::ANS_ACK){
				cout << "Newsgroup successfully created" << endl;
			}else{
				cout << "Newsgroup already exist" << endl;
			}
		}else if(toInt(command) == protocol::Protocol::ANS_DELETE_NG){
			string response;
			ss >> response;
			if(toInt(response) == protocol::Protocol::ANS_ACK){
				string nbrOfArticles;
				ss >> nbrOfArticles;
				cout << "The number of articles i the newsgroup are: "<< nbrOfArticles << endl;
			}else{
				cout << "Newsgroup does not exist" << endl;
			}
		}else if(toInt(command) == protocol::Protocol::ANS_LIST_ART){
			string response;
			ss >> response;
			if(toInt(response) == protocol::Protocol::ANS_ACK){
			//Skriva ut alla ariklar i newsgroupen
				cout << "Newsgroup successfully deleted" << endl;
			}else{
				cout << "Newsgroup does not exist" << endl;
			}
		}	
	}catch (ConnectionClosedException&) {
            cerr << "Server closed down!" << endl;
            exit(1);
        }

    }
}



